﻿/// <reference path="../jquery-1.5.1.min.js" />
/// <reference path="../ppds.js" />
/// <reference path="../global.variable.js" />


//sub article SubArticleAfterSucessCallBack
function SubArticleAfterSucessCallBack() {
    ApplyUploadify();
}

$(document).ready(function () {

    $('a.shadow-image').live('click', function () {
        //        var href = $(this).attr('href');
        //       
        //        Shadowbox.open({
        //            content: '<div id="welcome-msg">Welcome to my website!</div>',
        //            player: "html",
        //            title: "Welcome",
        //            height: 350,
        //            width: 350
        //        });
        //        return false;
    });

    $('.new-button').live('click', function () {
        var herf = $(this).attr('href');

        AjaxEditCallWithFormData(herf);
    });

    $('.add-new-button').live('click', function () {
        var herf = $(this).attr('href');
        var target = $(this).attr('target');
        // AjaxEditCallWithFormData(herf);
        AjaxCall(herf, target);
    });

    $('.logout-button').live('click', function () {
        LogOut();
    });

    $('.delete-button').live('click', function () {
        var herf = $(this).attr('href');
        var message = $(this).attr('message');
        AjaxDeleteCallWithFilter(herf, message);
    });


    //add new sub Article
    $('.add-articleContent-button').live('click', function () {
        var herf = $(this).attr('href');
        var target = $(this).attr('target');
        //AjaxCallAppendCallWithAfterSucessCallback(herf, target);
        AjaxCallAppendCallWithAfterSucessCallback(herf, target, NaN, SubArticleAfterSucessCallBack);
    });


    // delete sub article button
    $('.delete-centre-button').live('click', function () {
        var herf = $(this).attr('href');
        var message = $(this).attr('message');
        var target = $(this).attr('target');
        var clear = $(this).attr('clear');
        //AjaxDeleteCallWithFilter(herf, message);

        DeleteItem = function (sender) {
            AjaxCall(herf, target, clear);            
        };
        jQuery.Confirm(this, message, DeleteItem);
        return false;
    });


    //delete button upon thumbnails image
    $('.delete-article-image-button').live('click', function () {
        var herf = $(this).attr('href');
        var message = $(this).attr('message');
        var target = $(this).attr('target');
        var clear = $(this).attr('clear');
        //AjaxDeleteCallWithFilter(herf, message);

        DeleteItem = function (sender) {
            AjaxCall(herf, target);
            
        };
        jQuery.Confirm(this, message, DeleteItem);
        return false;
    });

    $('.delete-article-text-button').live('click', function () {
        var herf = $(this).attr('href');
        var message = $(this).attr('message');
        var target = $(this).attr('target');
        var clear = $(this).attr('clear');
        //AjaxDeleteCallWithFilter(herf, message);

        DeleteItem = function (sender) {
            //AjaxCall(herf, target);
            AjaxCallWithAfterSucessCallback(herf, target, NaN, SubArticleAfterSucessCallBack);
        };
        jQuery.Confirm(this, message, DeleteItem);
        return false;
    });


    $('form.article-content-form').live('submit', function () {
        return false;
    });

    //add multiple image 
    $(".add-IE-multiple-image-button").live('click', function () {
        var target = $(this).parents().eq(2);
        // alert(target.children().size());

        var size = $(target).children().size() - 3;
        // alert(size);
        //$(target).children().eq(2).clone().insertAfter(target.children().eq(2));
        $(target).children().eq(2).clone().insertAfter(target.children().eq(size));
    });



    $('.save-article-content-button').live('click', function () {



        var target = $(this).closest('div.articleContent');

        //  $(target).css('background-color', 'gray');

        var form = $(target).find('form:first');
        //var form = $('#frm_1');

        var action = jQuery.CreateAjaxUrl($(form).attr('action'));

        $(form).attr('action', action);
        CrruntTarget = target;
        //var data = form.serialize();




        var options = {
            //  target: target,   // target element(s) to be updated with server response 
            // beforeSubmit: showRequest,  // pre-submit callback 
            success: ArticleContentSucess, // post-submit callback 
            data: { "X-Requested-With": "XMLHttpRequest" }

            // other available options: 
            // url:       action         // override for form's 'action' attribute 
            //type:      type        // 'get' or 'post', override for form's 'method' attribute 
            //dataType: 'json'        // 'xml', 'script', or 'json' (expected server response type) 
            //clearForm: true        // clear all form fields after successful submit 
            //resetForm: true        // reset the form after successful submit 

            // $.ajax options can be used here too, for example: 
            //timeout:   3000                        
        };

        $(form).ajaxForm(options);
        try {
            $(form).ajaxSubmit(options);
        }
        catch (e) {

        }
        //$(form).ajaxSubmit(options);


        //AjaxPost(data, action, target);
        return false;
        //AjaxCallAppendCall(herf, target);
    });



    function ArticleContentSucess(data, textStatus, XMLHttpRequest, $form) {
        //  $form.clearForm();

        // var ajaxResponse = new jQuery.AjaxResponse(data);
        try {
            var s = String(data);
            if (s.indexOf('LogOut') > -1) {
                LogOut();
                //return false;
            }
            else if (s.indexOf('Server Error') > -1) {
                $(div_error_span).html(s);
                $(div_error_span).find('style').remove();
                $(div_error).show();
                myLayout.open('south');
            }
            else {
                $(CrruntTarget).html(data);
                //apply uploadify for multiple file upload
                SubArticleAfterSucessCallBack();
            }


            // RemoveAddMultipleImageButton();
            //ReloadScrollPosition();
        }
        catch (ex) {
            LogOut();
        }
        return false;


    }





});                                                                         //end of doc ready